//
//  StatzProtocols.h
//  Statz
//
//  Created by Dave MacLachlan on 07/07/23.
//
// Copyright 2007 Google Inc.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations under
// the License.
//

typedef enum {
  kStatzStateNone = 0,
  kStatzStateAvailable = 1 << 0,
  kStatzStateAway = 1 << 5,
  kStatzStateDoNotDisturb = 1 << 10,
  kStatzStateContactMe = 1 << 15,
  kStatzStateInvisible = 1 << 20,
  kStatzStateOffline = 1 << 25,
  kStatzStateMixed = 1 << 28,
} StatzState;

typedef enum {
  kStatzUpdateSuccess,
  kStatzUpdateNotChanged,
  kStatzUpdateError
} StatzUpdateValue;

@class Status;
@class StatusUpdate;

@protocol StatzSinkPlugin <NSObject>
- (NSString*)uuid;
- (NSString*)appBundleID;
- (NSString*)serverName;
- (NSString*)name;
- (NSImage*)image;
- (Status*)currentStatus;
- (StatzUpdateValue)updateStatz:(StatusUpdate*)statusUpdate;
- (void)updateStatusThread:(StatusUpdate*)statusUpdate;
- (NSWindow*)preferencesWindow;
- (BOOL)hasPreferences;
- (void)showAccountPreferences:(id)sender;
- (IBAction)closePreferences:(id)sender;
- (void)savePreferences;
- (void)cancelPreferences;
- (unsigned int)stateMenuMask;
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;
@end

#if DEBUG_ASSERT_PRODUCTION_CODE
  #define require_curl_noerr(errorCode, exceptionLabel)                   \
    do {                                                                  \
      if ( CURLE_OK != (errorCode) ) {                                    \
        goto exceptionLabel;                                              \
      }                                                                   \
    } while ( 0 )
#else
  #define require_curl_noerr(errorCode, exceptionLabel)                   \
    do {                                                                  \
      int evalOnceErrorCode = (errorCode);                                \
      if ( CURLE_OK != evalOnceErrorCode ) {                              \
        DEBUG_ASSERT_MESSAGE(DEBUG_ASSERT_COMPONENT_NAME_STRING,          \
                             #errorCode " == CURLE_OK ",                  \
                             #exceptionLabel,                             \
                             0,                                           \
                             __FILE__,                                    \
                             __LINE__,                                    \
                             evalOnceErrorCode);                          \
        goto exceptionLabel;                                              \
      }                                                                   \
    } while ( 0 )
#endif

#if DEBUG_ASSERT_PRODUCTION_CODE
  #define require_ae_noerr(appleEventDesc, exceptionLabel)                \
    do {                                                                  \
      NSAppleEventDescriptor *desc = (appleEventDesc);                    \
      if ( nil == desc ) {                                                \
        goto exceptionLabel;                                              \
      }                                                                   \
      NSAppleEventDescriptor *error;                                      \
      error = [desc descriptorForKeyword:keyErrorNumber];                 \
      if (error && [error int32Value] != noErr) {                         \
        goto exceptionLabel;                                              \
      }                                                                   \
    } while ( 0 )
#else
  #define require_ae_noerr(appleEventDesc, exceptionLabel)                \
    do {                                                                  \
      NSAppleEventDescriptor *desc = (appleEventDesc);                    \
      if ( nil == desc ) {                                                \
        DEBUG_ASSERT_MESSAGE(DEBUG_ASSERT_COMPONENT_NAME_STRING,          \
                             "AESend Failed",                             \
                             #exceptionLabel,                             \
                             0,                                           \
                             __FILE__,                                    \
                             __LINE__,                                    \
                             errAENotAEDesc);                             \
        goto exceptionLabel;                                              \
      }                                                                   \
      NSAppleEventDescriptor *error;                                      \
      error = [desc descriptorForKeyword:keyErrorNumber];                 \
      if (error) {                                                        \
        Int32 errorVal = [error int32Value];                              \
        if (errorVal != noErr) {                                          \
          DEBUG_ASSERT_MESSAGE(DEBUG_ASSERT_COMPONENT_NAME_STRING,        \
                               "AppleEvent Error",                        \
                               #exceptionLabel,                           \
                               0,                                         \
                               __FILE__,                                  \
                               __LINE__,                                  \
                               errorVal);                                 \
        }                                                                 \
        goto exceptionLabel;                                              \
      }                                                                   \
    } while ( 0 )
#endif


